package cn.edu.ujn.liuklab3.service;

import cn.edu.ujn.liuklab3.entity.User;
import cn.edu.ujn.liuklab3.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;


    @Override
    public User queryById(Integer userId) {
        return this.userDao.queryById(userId);
    }

    @Override
    public Page<User> queryByPage(User user, PageRequest pageRequest) {
        long total = this.userDao.count(user);
        return new PageImpl<>(this.userDao.queryAllByLimit(user, pageRequest), pageRequest, total);
    }

    @Override
    public User insert(User user) {
        this.userDao.insert(user);
        return user;
    }

    @Override
    public User update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getUserId());
    }

    @Override
    public boolean deleteById(Integer userId) {
        return this.userDao.deleteById(userId) > 0;
    }

    @Override
    public cn.edu.ujn.util.Page<User> selectAll(User user) {
        List<User> users = userDao.selectAll(user);
        cn.edu.ujn.util.Page<User> page = new cn.edu.ujn.util.Page<>();
        page.setPage(user.getStart());
        page.setRows(users);
        page.setSize(user.getRows());
        page.setTotal(userDao.selectCount());
        return page;
    }

    @Override
    public User checkLogin(User user) {
        return userDao.selectByUser(user);
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }
}
